package com.yunkeji.api.msg.controller;

import com.yunkeji.api.msg.bo.SendBo;
import com.yunkeji.api.msg.service.MsgService;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.enums.app.AccountTypeEnums;
import com.yunkeji.common.helper.LoginHelper;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.config.annotation.ApiAuth;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * 文字消息
 *
 * @author 老张
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/msg")
public class MsgController extends BaseController {
    private final MsgService msgService;
    /**
     * 客服给用户发消息
     *
     * @author 老张
     */
    @PostMapping("/serviceToUser")
    @ApiAuth(allow = {AccountTypeEnums.客服})
    public R serviceToUser(@Valid @RequestBody SendBo bo) {
        bo.setSendUserId(LoginHelper.getUserId());
        String send = msgService.serviceToUser(bo);
        return R.ok("发送成功", send);
    }
    /**
     * 用户给客服发消息
     *
     * @author 老张
     */
    @PostMapping("/userToService")
    public R userToService(@Valid @RequestBody SendBo bo) {
        bo.setSendUserId(LoginHelper.getUserId());
        String send = msgService.userToService(bo);
        return R.ok("发送成功", send);
    }
    /**
     * 1.发送
     *
     * @author 老张
     */
    @PostMapping("/send")
    @RepeatSubmit(interval = 3, timeUnit = TimeUnit.SECONDS, message = "发送频率过快")
    public R send(@Valid @RequestBody SendBo bo) {
        bo.setLockId(StringUtils.getSortIdStr(LoginHelper.getUserId(), bo.getRecUserId()));
        String send = msgService.send(bo);
        return R.ok("发送成功", send);
    }

    /**
     * 用户打开在线客服后的自动提醒
     *
     * @author 雷擎宇
     */
    @PostMapping("/kefuRemind")
    public R kefuRemindUser(@RequestBody SendBo bo) {
        msgService.kefuRemindUser(bo);
        return R.ok("发送成功", "您好，请问有什么能帮到您？");
    }
}
